﻿
//***********************************************************************
// * Module:   TbBstDicHandler
// * 作者:  曹荣芝
// * Modified: 2013/5/4
// * Purpose: Definition of the Class TbBstDicHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.JWBST.JWBSTBLL;
using XWG.Framework;
using System.Collections;
using JWBSTAPP.CommonClass;
namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// TbBstDicHandler 的摘要说明
    /// </summary>
    public class TbBstDicHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {

                TbBstDic mTbBstDic = new TbBstDic();
                mTbBstDic.Id = Guid.NewGuid().ToString(); //主键
                mTbBstDic.Code = context.Request.Form["texCode"]; //字典编码
                mTbBstDic.Description = context.Request.Form["texDescription"]; //字典描述
                mTbBstDic.Parentnode = context.Request.Form["selParentnode"]; //字典父节点(对应DIC_COLLECT表的code字段)
                mTbBstDic.Deltag = 0; //删除标志
                mTbBstDic.Createdate = DateTime.Now;////添加时间
                mTbBstDic.Remark = context.Request.Form["texRemark"];
                // mTbBstDic.Deltime = context.Request.Form["texDeltime"]; //删除时间

                mResultModel = TbBstDicBLL.Insert(mTbBstDic, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {

            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            try
            {
                string[] arryParamKey = context.Request.Params["id"].ToString().Split(',');
                if (arryParamKey.Length == 0)
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                TbBstDic mTbBstDic = new TbBstDic();
                foreach (var paramKey in arryParamKey)
                {
                    mResultModel = TbBstDicBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); ; //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, ""); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }

        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            TbBstDic mTbBstDic = new TbBstDic();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mTbBstDic = TbBstDicBLL.Load(paramKey); //根据键值获取实体信息
                if (mTbBstDic != null)
                {
                    mTbBstDic.Code = context.Request.Form["texCode"]; //字典编码
                    mTbBstDic.Description = context.Request.Form["texDescription"]; //字典描述
                    mTbBstDic.Parentnode = context.Request.Form["selParentnode"]; //字典父节点(对应DIC_COLLECT表的code字段)
                    mTbBstDic.Deltag = 0; //删除标志
                    //mTbBstDic.Createdate = DateTime.Now;////添加时间
                    mTbBstDic.Remark = context.Request.Form["texRemark"];

                    mResultModel = TbBstDicBLL.Update(mTbBstDic, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;

                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = "";
            try
            {
                TbBstDic mTbBstDic = new TbBstDic(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mTbBstDic = TbBstDicBLL.Load(paramKey); //根据键值获取管理员
                    if (mTbBstDic != null)
                    {
                        json = JsonConvert.SerializeObject(mTbBstDic);
                        ////过滤时间
                        if (!String.IsNullOrEmpty(mTbBstDic.Createdate.ToString()))
                        {
                            Hashtable hashtable = new Hashtable();
                            hashtable.Add("Createdate", mTbBstDic.Createdate.ToString().Replace("T", " "));
                            json = JsonString.AddAttribute(json, hashtable);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, "");
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }
            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];
            string code = context.Request.Form["texCode"]; ////字典编码
            string describe = context.Request.Form["TexDescribe"]; ////字典描述
            string selCode = context.Request.Form["selCode"]; ////字典编码
            string timeStart = context.Request.Form["texTimeStart"]; ////添加时间-开始
            string timeEnd = context.Request.Form["texTimeEnd"]; ////添加时间-结束
            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
                return;
            DataTable dtTbBstDic = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage Q = new QueryByPage();
            SqlConditionList sqllist = new SqlConditionList();
            string sqlWhere = " deltag=0 ";
            if (!string.IsNullOrEmpty(code)) ////字典编码
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstDicConst.Columns.Code, '%' + code + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(describe)) ////字典描述
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, TbBstDicConst.Columns.Description, '%' + describe + '%'), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(selCode)) ////字典编码
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, TbBstDicConst.Columns.Parentnode, selCode), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(timeStart)) ////添加时间-开始
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_LESS_THAN, TbBstDicConst.Columns.Createdate, Convert.ToDateTime(timeStart)), SqlLogicOper.AND_LOGIC);
            }
            if (!string.IsNullOrEmpty(timeEnd)) ////添加时间-结束
            {
                sqllist.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.NOT_MORE_THAN, TbBstDicConst.Columns.Createdate, Convert.ToDateTime(timeEnd)), SqlLogicOper.AND_LOGIC);
            }
            if (string.IsNullOrEmpty(sqllist.ToSql()))
            {
                sqlWhere = "where" + sqlWhere;
            }
            else
            {
                sqlWhere = sqllist.ToSql() + " and " + sqlWhere;
            }
            dtTbBstDic = Q.GetRecordByPage("ID,Code,PARENTNODE,Description,VDescription,Createdate ", sqlWhere, " createdate", intPageSize, intPageIndex, TbBstDicConst.QTableName, TbBstDicConst.FieldKey, "desc").Tables[0];
            if (dtTbBstDic != null && dtTbBstDic.Rows.Count > 0)
            {
                for (int i = 0; i < dtTbBstDic.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtTbBstDic.Columns[i].Caption;
                    list.Add(hm);
                }
                tm.Header = list;
                tm.data = dtTbBstDic;
                tm.totalCount = Convert.ToString((int)Q.RowCount);
            }
            IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
            string json = JsonConvert.SerializeObject(tm, dtConverter);
            context.Response.Write(json);
        }
        #endregion

        #region 绑定审核状态下拉框
        /// <summary>
        /// 绑定下拉列表-审核状态下拉框
        /// </summary>
        protected void QueryRecordForDrop()
        {
            List<ControlID> selResPid = new List<ControlID>();

            ControlID CID = new ControlID();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            QueryByPage Q = new QueryByPage();
            string sql = " where parentnode='audit' ";
            DataTable dt = new DataTable();
            dt = Q.GetRecordByPage("*", sql, "", 0, 1, TbBstDicConst.TableName, TbBstDicConst.FieldKey, "").Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CID = new ControlID();
                    CID.name = Convert.ToString(dt.Rows[i][TbBstDicConst.Columns.Description]);
                    CID.value = Convert.ToString(dt.Rows[i][TbBstDicConst.Columns.Code]);
                    selResPid.Add(CID);
                }
            }

            string json = JsonConvert.SerializeObject(selResPid);
            context.Response.Write(json);
        }

        #endregion

        #region 获取警种下拉框
        /// <summary>
        /// 获取警种下拉框
        /// </summary>
        protected void SelPolice()
        {
            List<ControlID> selResPid = new List<ControlID>();

            ControlID CID = new ControlID();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            QueryByPage Q = new QueryByPage();
            string sql = " where parentnode='JZ' and deltag='0'";
            DataTable dt = new DataTable();
            dt = Q.GetRecordByPage("*", sql, "", 0, 1, TbBstDicConst.TableName, TbBstDicConst.FieldKey, "").Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CID = new ControlID();
                    CID.name = Convert.ToString(dt.Rows[i][TbBstDicConst.Columns.Description]);
                    CID.value = Convert.ToString(dt.Rows[i][TbBstDicConst.Columns.Code]);
                    selResPid.Add(CID);
                }
            }

            string json = JsonConvert.SerializeObject(selResPid);
            context.Response.Write(json);
        }

        #endregion

        #region 获取组织机构下拉框
        /// <summary>
        /// 获取组织机构下拉框
        /// </summary>
        protected void SelType()
        {
            List<ControlID> selResPid = new List<ControlID>();

            ControlID CID = new ControlID();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            QueryByPage Q = new QueryByPage();
            string sql = " where parentnode='Agency' and deltag='0'";
            DataTable dt = new DataTable();
            dt = Q.GetRecordByPage("*", sql, "", 0, 1, TbBstDicConst.TableName, TbBstDicConst.FieldKey, "").Tables[0];
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    CID = new ControlID();
                    CID.name = Convert.ToString(dt.Rows[i][TbBstDicConst.Columns.Description]);
                    CID.value = Convert.ToString(dt.Rows[i][TbBstDicConst.Columns.Code]);
                    selResPid.Add(CID);
                }
            }

            string json = JsonConvert.SerializeObject(selResPid);
            context.Response.Write(json);
        }

        #endregion

    }
}


